const app = getApp();
const baseURL = app.globalData.baseURL;
Page({
  data: {
    currentScore: 0, // 当前选中星数
    normalSrc: '/images/star_gray.png',
    selectedSrc: '/images/star_full.png',
    evaluate: {},
    checked: false
  },
  handleStarClick(e) {
    const score = e.currentTarget.dataset.index;
    this.setData({ currentScore: score });
  },
  textareaInput(e) {
    this.data.evaluate.evaluateContent = e.detail.value;
  },
  checkbox() {
    this.data.checked = !this.data.checked;
  },
  submit() {
    this.data.evaluate.evaluateGrade = this.data.currentScore;
    this.data.evaluate.evaluateState = this.data.checked ? 1 : 0;
    wx.request({
      url: `${baseURL}/evaluate/evaluate/insert`,
      method: 'POST',
      data: this.data.evaluate,
      header: {
        'Authorization': wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '提交成功',
          })
        }
      }
    })
  },
  onLoad(option) {
    this.data.evaluate.coachId = option.coachId;
    this.data.evaluate.reservationId = option.reservationId;
    wx.request({
      url: `${baseURL}/evaluate/evaluate/selByUidAndCoachId`,
      method: 'POST',
      header: {
        'Authorization': wx.getStorageSync('token')
      },
      data: option.coachId,
      success: (res) => {
        if (res.data.code == 200) {
          this.setData({
            evaluate: res.data.data,
            currentScore: res.data.data.evaluateGrade,
            checked: res.data.data.evaluateState == 1 ? true : false
          })
        }
      }
    })
  }
})